package portal.manager.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 
 * @author Sorrentino
 *
 */
public class Template {
	
	/**
	 * 
	 * @author Sorrentino
	 *
	 */
	@SuppressWarnings("serial")
	public static class Variable implements Serializable {
		
		private String id;
		private String templateId;
		private String name;
		private boolean block = false;
		private boolean codeModule = false;
		
		public Variable() {
			
		}
		public Variable( Template tpl, String name, boolean block ) {
			assert null!=tpl;
			assert null!=name;
			
			if( null==tpl ) throw new IllegalArgumentException( "template is null!");
			if( null==name ) throw new IllegalArgumentException( "name is null!");
			
			this.setId( UUID.randomUUID().toString() );
			this.setTemplateId( tpl.getName() );
			this.setName( name );
			this.setBlock(block);
			
		}
		public Variable( Template tpl, String name, boolean block, boolean codeModule ) {
			this( tpl, name, block);
			this.setCodeModule(codeModule);
		}
		
		public final String getId() {
			return id;
		}
		public final void setId(String id) {
			this.id = id;
		}
		public final String getTemplateId() {
			return templateId;
		}
		public final void setTemplateId(String templateId) {
			this.templateId = templateId;
		}
		public final String getName() {
			return name;
		}
		public final void setName(String name) {
			this.name = name;
		}
		public final boolean isBlock() {
			return block;
		}
		public final void setBlock(boolean block) {
			this.block = block;
		}
		public final boolean isCodeModule() {
			return codeModule;
		}
		public final void setCodeModule(boolean codeModule) {
			this.codeModule = codeModule;
		}
	}
	
	
	private String name;
	private String source;
	private List<? extends Variable> variables;
	
	public Template() {
		
	}
	
	public Template( String name, String source ) {
		this.name = name;
		this.source = source;
	}
	
	public final String getName() {
		return name;
	}
	
	public final void setName(String value) {
		this.name = value;
	}
	
	public final String getSource() {
		return source;
	}

	public final void setSource(String source) {
		this.source = source;
	}

	public final Variable[] getVariableArray() {
		
		if( null==variables ) return new Variable[0];
		
		return variables.toArray( new Variable[variables.size()]);
		
	}
	
	public final List<? extends Variable> getVariables() {
		return variables;
	}

	public final void setVariables(List<? extends Variable> variables) {
		this.variables = variables;
	}
	
	
	@SuppressWarnings("unchecked")
	public void addVariable( Variable value ) {
		if( null==variables ) {
			variables = new ArrayList<Variable>(10);
		}
		((List<Variable>)variables).add(value);
	}

}
